package org.gdufe.logs.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.gdufe.logs.mapper.LogMapper;
import org.gdufe.pojos.dto.InsertLogDto;
import org.gdufe.pojos.pojo.Log;
import org.gdufe.pojos.dto.PageParams;
import org.gdufe.pojos.vo.PageResponseResult;
import org.gdufe.pojos.vo.R;
import org.gdufe.logs.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Override
    public PageResponseResult querry(PageParams pageParams, String username,Integer bId) {
        if (pageParams==null||pageParams.getPageNo()<=0||pageParams.getPageSize()<=0){
            pageParams=new PageParams();
        }
        QueryWrapper<Log> wrapper=new QueryWrapper<Log>();
        if (username!=null&&!username.isEmpty()){
            wrapper.like("username",username);
        }
        if (bId!=null&&bId>0){
            wrapper.eq("b_id",bId);
        }
        //分页对象
        Page<Log> page = new Page<>(pageParams.getPageNo(), pageParams.getPageSize());
        IPage<Log> pageResult = logMapper.selectPage(page, wrapper);
        // 获取数据列表
        List<Log> list = pageResult.getRecords();
        // 获取数据总数
        long total = pageResult.getTotal();
        // 构建结果集
        PageResponseResult result=new PageResponseResult((int) pageResult.getCurrent(), (int) pageResult.getSize(), (int) pageResult.getTotal());
        result.setData(list);
        return result;
    }

    @Override
    public R insertLog(InsertLogDto dto) {
        Integer bId = dto.getBId();
        Integer uId = dto.getUId();
        String title = dto.getTitle();
        String username = dto.getUsername();
        if (bId==null||title==null||title.isEmpty()||username==null||username.isEmpty()){
            return R.errorResult(500,"请确保参数正确");
        }
        Log log=new Log();
        log.setBId(bId);
        if (uId!=null)
        log.setUId(uId);
        log.setTitle(title);
        log.setUsername(username);
        log.setStatus(0);
        LocalDateTime now=LocalDateTime.now();
        System.out.println(now);
        log.setBorrowTime(now);
        int insert = logMapper.insert(log);
        if (insert<=0){
            return R.errorResult(500,"插入失败");
        }
        return R.okResult(200,"插入成功");
    }

    @Override
    public R loss(Integer id) {
        Log log = logMapper.selectById(id);
        if(log==null){
            return R.errorResult(500,"目标记录不存在");
        }
        Integer status = log.getStatus();
        if (status==2){
            return R.errorResult(500,"已挂失");
        }
        log.setStatus(2);
        int i = logMapper.updateById(log);
        if (i<=0){
            return R.errorResult(500,"挂失失败");
        }
        return R.okResult(200,"挂失成功");
    }


    @Override
    public R borrowLog(Log log) {
        Integer bId = log.getBId();
        String title = log.getTitle();
        String username = log.getUsername();
        LocalDateTime borrowTime = log.getBorrowTime();
        if (bId==null||title==null||title.isEmpty()||username==null||username.isEmpty()||borrowTime==null){
            return R.errorResult(500,"参数错误");
        }
        log.setStatus(0);//设为借出
        int insert = logMapper.insert(log);
        if (insert<=0){
            return R.errorResult(500,"添加借阅记录失败");
        }
        return R.okResult(200,"添加借阅记录成功");
    }


    @Override
    public R returnLogById(Integer id) {
        Log log = logMapper.selectById(id);
        if(log==null){
            return R.errorResult(500,"目标记录不存在");
        }
        Integer status = log.getStatus();
        if (status==1){
            return R.errorResult(500,"已归还，请勿重复归还");
        }
        log.setStatus(1);
        int i = logMapper.updateById(log);
        if (i<=0){
            return R.errorResult(500,"归还失败");
        }
        return R.okResult(200,"归还成功");
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public R returnLog(Log log) {
        String username = log.getUsername();
        Integer bId = log.getBId();
        String title = log.getTitle();
        LocalDateTime returnTime = log.getReturnTime();

        if (bId==null||returnTime==null||title==null||title.isEmpty()||username==null||username.isEmpty()){
            return R.errorResult(500,"参数错误");
        }
        QueryWrapper<Log> wrapper=new QueryWrapper<>();
        wrapper.eq("b_id",bId).eq("username",username).eq("status",0);
        Log log0 = logMapper.selectOne(wrapper);
        if (log0==null){
            return R.errorResult(500,"目标日志不存在，添加归还日志失败");
        }
        log0.setReturnTime(returnTime);
        log0.setStatus(1);
        int i = logMapper.updateById(log0);
        if (i<=0){
            return R.errorResult(500,"添加归还日志失败");
        }else return R.okResult(200,"添加归还日志成功");
    }

}
